package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.UserService;
import com.itheima.reggie.utils.SMSUtils;
import com.itheima.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author solang
 * @date 2022-12-30 9:51
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session) {
        //获取手机号
        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)) {
            //生成随机验证码
            String code = ValidateCodeUtils.generateValidateCode(6).toString();

            //调用阿里云提供短信服务API完成发送短信
//            SMSUtils.sendMessage("瑞吉外卖",phone,code);
            //将验证码存储到session中
            session.setAttribute(phone, code);
//            System.out.println("存储的验证码:"+code);
//            redisTemplate.opsForValue().set(phone,code,5 ,TimeUnit.MINUTES);
            return R.success("验证发送成功");
        }
        return R.error("发送失败");
    }

    /**
     * 移动端登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response) {
        log.info("user:{}", user);
        //根据手机号查询
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();

        redisTemplate.opsForValue().set(user.getPhone(),"1111",5 ,TimeUnit.MINUTES);
//        redisTemplate.opsForValue().set("city111","hainan");
        Object o = redisTemplate.opsForValue().get(user.getPhone());
        System.out.println("获取存储在redis里面的验证吗为:"+o);
        queryWrapper.eq(User::getPhone, user.getPhone());
        User one = userService.getOne(queryWrapper);
        if (one == null) {
            User userPhone = new User();
            userPhone.setPhone(user.getPhone());
            userService.save(user);
            request.getSession().setAttribute("user",userPhone);
            return R.success(userPhone);
        }
        request.getSession().setAttribute("user",one);
//        redisTemplate.delete(user.getPhone());

        return R.success(one);
    }

}
